<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AngularJS&middot;Hello AngularJS</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
    <style>
        .demo{
            margin-bottom:10px;
        }
        .statusBar{
            display: inline-block;
            text-align: center;
            width: 60px;
            height:60px;
            line-height: 60px;
            border-radius: 30px;
            background:#999;
        }
    </style>
</head>
<body>

 <div class="container">
     <div class="row">
         <nav class="navbar navbar-default navbar-static-top">
             <div class="container-fluid">
                 <div class="navbar-header">
                     <a class="navbar-brand" href="../index.html">首页</a>
                 </div>
                 <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0)">Link</a></li>
                    <li><a href="javascript:void(0)">Link2</a></li>
                 </ul>
             </div>
         </nav>
     </div>
     <h3>http://www.npr.org/音频播放示例,参考：http://blog.jobbole.com/50022/</h3>
     <div class="row demo" ng-app="VideoAPP">
          <div class="row" ng-controller="PlayerController">
              <p class="text-center">
                  <span class="statusBar" ng-show="!playing">
                      <i class="glyphicon glyphicon-play-circle"></i><br>
                      等待播放
                  </span>
                  <span class="statusBar"  ng-show="playing">
                      <i class="glyphicon glyphicon-pause"></i><br>
                      播放中...
                  </span>
              </p>
              <ul id="programs_list" class="list-group">
                  <li class="list-group-item" ng-repeat="program in programs">
                      <span npr-link play='play(program)' stop='stop()' ng-model="program"></span>
                  </li>
              </ul>
          </div>
     </div>
 </div>
</body>
</html>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script>
    var apiKey = 'MDIyODg2ODk4MDE0NTYxMzI5NjBhNmRlYg000',
        nprUrl = 'http://api.npr.org/query?id=61&fields=relatedLink,title,byline,text,audio,image,pullQuote,all&output=JSON';

    var app=angular.module("VideoAPP",[]);
    var audio = document.createElement('audio');

    app.factory('audio', ['$document', function($document) {
        var audio = $document[0].createElement('audio');
        return audio;
    }]);

    app.controller("PlayerController",function($scope,$http,audio){
        $http({
            method: 'JSONP',
            url: nprUrl + '&apiKey=' + apiKey + '&callback=JSON_CALLBACK'
        }).success(function(data, status) {
            console.log(data);
            $scope.programs = data.list.story;
        }).error(function(data, status) {
            alert("加载失败!");
        });

        $scope.playing = false;

        $scope.play = function(program) {
            //format.mp4.$text是NPR API给我们的到这个音频mp4文件的路径 $scope.play = function(program) {
            if ($scope.playing) audio.pause();
            var url = program.audio[0].format.mp4.$text;
            audio.src = url;
            audio.play();
            //储存播放器的状态为正在播放
            $scope.playing = true;
        };
        $scope.stop = function() {
           audio.pause();
            $scope.playing = false;
        };
        audio.addEventListener('ended', function() {
            $scope.$apply(function() {
                $scope.stop()
            });
        });
    });


    //指令scope请参考http://www.linuxidc.com/Linux/2015-05/116924.htm
    app.directive('nprLink', function() {
        return {
            restrict: 'EA',
            require: ['^ngModel'], //要求必须有ng-model指令
            replace: true,
            scope: {  //指令属性
                ngModel: '=', //指令中的与ng-model设置的对象相等
                play: '&',  //作为方法传入指令,
                stop:'&'
            },
            templateUrl: 'views/nprListItem.html',
            link: function(scope, ele, attr) {
                scope.duration = scope.ngModel.audio[0].duration.$text;
            }
        }
    });

    app.controller('RelatedController', ['$scope', function($scope) {

    }]);
</script>
